import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:photo_manager/photo_manager.dart';

class CoverImageItem extends StatelessWidget {
  final AssetEntity entity;
  final int size;

  const CoverImageItem({
    Key key,
    this.entity,
    this.size = 512,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return FutureBuilder<Uint8List>(
      future: entity.thumbDataWithSize(size, size),
      builder: (BuildContext context, AsyncSnapshot<Uint8List> snapshot) {
        var futureData = snapshot.data;
        if (snapshot.connectionState == ConnectionState.done &&
            futureData != null) {
          return _buildImageItem(context, futureData);
        }
        return Container();
      },
    );
  }

  Widget _buildImageItem(BuildContext context, Uint8List data) {
    return Stack(
      children: [
        Image.memory(
          data,
          width: double.infinity,
          height: double.infinity,
          fit: BoxFit.cover,
        ),
        entity.type == AssetType.video
            ? Align(
                alignment: Alignment.center,
                child: Icon(
                  Icons.play_arrow_rounded,
                  color: Colors.white,
                ),
              )
            : Container(),
      ],
    );
  }
}
